<?php
global $smarty;

$thisPage = "company_email";

TemplateHelper::useLayout("default", true);

addDataService("getcompanies");
addDataService("sendmail");
addDataservice("filtergroup");
addDataService("getcountries");

function buildLayout($smarty) {
    $smarty->assign("default", array(
        content => getContent($smarty),
    ));
}

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "getcompanies" : { return service_getcompanies($params); }
        case "filtergroup" : { return service_filtergroup($params); }
        case "sendmail" : { return service_sendmail($params); }
        case "getcountries" : { return service_getcountries($params); }
        case "sendtestemail" : { return service_sendtestemail($params); }
    }
}

function service_getcountries($params) {
    Database::loadModel("companies");
    $result = Database::getModel()->getCountries();
    return serviceSuccessful(array(
        items => $result,
    ));
}

function service_getcompanies($params) {
    Database::loadModel("companies");
    Database::getModel()->setPageData(getRequestParameter("pg"), getRequestParameter("rows"));
    $result = Database::getModel()->getCompanyEmails();
    $reccount = Database::getModel()->getCompanyEmailCount();
    $limit = intval(getRequestParameter("rows"), 10);
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($result, "company_id"),
    );
}

function service_sendtestemail($params) {
    $templates = array(
        firstname => "Test",
        lastname => "User",
        title => "Dr.",
        companyname => "Test company",
        link => "test link",
        username => "testuser",
        password => "testpassword",
    );
    $response = sendEmailWithContent($params['email'], $params['subject'], $params['text'], array(), $templates);
    return serviceSuccessful(array());
}

function service_filtergroup($params) {
    $result = array();
    Database::loadModel("companies");
    if ($params['group'] == "1") {
        $result['rows'] = Database::getModel()->getNoHomepageCompanies();
    } else if ($params['group'] == "2") {
        $result['rows'] = Database::getModel()->getRegisteredCompanies();
    } else if ($params['group'] == "3") {
        $result['rows'] = Database::getModel()->getBusinessLeaders();
    } else if ($params['group'] == "4") {
        if ($params['hstype'] == "hs2") {
            $result['rows'] = Database::getModel()->getCompaniesByHs2($params['hsvalue']);
        } else {
        }
    } else if ($params['group'] == "5") {
        $result['rows'] = Database::getModel()->getExporterImporterHungarianCompanies();
    } else if ($params['group'] == "6") {
        $result['rows'] = Database::getModel()->getHungarianCompaniesAbroad($params['country']);
    } else if ($params['group'] == "11") {
        $result['rows'] = Database::getModel()->getNotUpdatedCompanies();
    } else if ($params['group'] == "12") {
        Database::loadModel("user");
        $result['rows'] = Database::getModel()->getVisitors();
    } else if ($params['group'] == "13") {
        Database::loadModel("vw_top600companies");
        $result['rows'] = Database::getModel()->getList();
    } else if ($params['group'] == "14") {
        Database::loadModel("vw_newcompanies");
        $result['rows'] = Database::getModel()->getList();
    }
    return serviceSuccessful(array(
        griddata => $result,
    ));
}

function service_sendmail($params) {
    $prms = $params['company_ids'];
    if ($params['group'] == "13") {
        Database::loadModel("vw_top600companies");
        $prms = $params['emailcount'];
    } else if ($params['group'] == "14") {
        Database::loadModel("vw_newcompanies");
        $prms = $params['emailcount'];
    } else {
        Database::loadModel("companies");
    }
    $emailList = Database::getModel()->getCompanyEmails($prms);
    $response = "";
    $errorMsg = "";
    for ($i = 0; $i < count($emailList); $i++) {
            $response = sendEmailWithContent($emailList[$i]['email1'], $params['subject'], $params['text'], array(), array(
                companyname => $emailList[$i]['companyname'],
            ));
        if ($response != "") {
            $errorMsg = $response;
        } else {
            if ($params['group'] == "13") {
                Database::getModel()->setState($emailList[$i]['id'], 2);
            } else if ($params['group'] == "14") {
                Database::getModel()->setState($emailList[$i]['id'], 2);
            }
        }
        if ($emailList[$i]['email2'] != "") {
            $response = sendEmailWithContent($emailList[$i]['email2'], $params['subject'], $params['text'], array(), array(
                companyname => $emailList[$i]['companyname'],
            ));
            if ($response != "") {
                $errorMsg = $response;
            } else {
                if ($params['group'] == "13") {
                    Database::getModel()->setState($emailList[$i]['id'], 2);
                } else if ($params['group'] == "14") {
                    Database::getModel()->setState($emailList[$i]['id'], 2);
                }
            }
        }
    }
    if ($errorMsg == "") {
        return serviceSuccessful(array(
        ));
    } else {
        return serviceFailure(array(), $errorMsg);
    }
}

function getContent($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content", true));
}

function getContextHelp($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contexthelp", true));
}

function getPageHeader() {
}

function getPageFooter() {
}

?>
